package com.mall.weapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mall.weapp.mapper.OrderMapper;
import com.mall.weapp.mapper.ProductMapper;
import com.mall.weapp.model.entity.Order;
import com.mall.weapp.model.entity.Product;
import com.mall.weapp.model.param.AddOrderParam;
import com.mall.weapp.model.vo.CartItemVo;
import com.mall.weapp.model.vo.CartVo;
import com.mall.weapp.model.vo.OrderListVo;
import com.mall.weapp.service.CartService;
import com.mall.weapp.service.OrderService;
import com.mall.weapp.util.RedisUtil;
import io.netty.util.internal.StringUtil;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private ProductMapper productMapper;

    @Resource
    private CartService cartService;

    @Resource
    private RedisUtil redisUtil;

    @Override
    public int create(AddOrderParam orderParam) {

        String userId = orderParam.getUserId();
        Map<String, Integer> productItem = orderParam.getProductItem();
        String key = "user:" + userId + ":cart";
        AtomicReference<String> item = new AtomicReference<>();
        AtomicReference<Double> price = new AtomicReference<>(0.00);
        AtomicReference<String> plus = new AtomicReference<>();
        productItem.forEach((k, v) -> {
            Product product = productMapper.selectById(k);
            price.set(price.get() + product.getPrice() * v);
            item.set((item.get() == null ? "" : item) + k + ",");
            plus.set((plus.get() == null ? "" : plus)+""+v+",");
            redisUtil.setRemove(key, k);
        });

        Order order = new Order();
        order.setProductItem(item.get());
        order.setUserId(orderParam.getUserId());
        order.setTotalPrice(price.get());
        order.setCreateTime(new Date());
        order.setAddressId(orderParam.getAddressId());
        order.setStatus(orderParam.getStatus());
        order.setPaymentStatus(orderParam.getPaymentStatus());
        order.setProductNum(plus.get());
        return orderMapper.insert(order);
    }

    @Override
    public List<OrderListVo> getList(String userId, String orderStatus) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        if (!StringUtil.isNullOrEmpty(orderStatus)) {
            wrapper.eq("status", orderStatus);
        }
        List<Order> orders = orderMapper.selectList(wrapper);
        List<OrderListVo> productList = new ArrayList<>();

        if (!orders.isEmpty()) {
            orders.forEach(order -> {
                String[] items = order.getProductItem().split(",");
                String[] names = order.getProductNum().split(",");
                int i = 0;
                List<Product> productList1 = productMapper.selectBatchIds(Arrays.asList(items));
                for (Product product : productList1) {
                    productList.add(new OrderListVo(product, order.getStatus(), names[i++]));
                }
            });
            return productList;
        }
        return productList;
    }
}
